package com.yonyou.brigade.third_parties.fanscar.controller;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.third_parties.fanscar.model.FanscarConfig;
import com.yonyou.brigade.third_parties.fanscar.service.IFanscarOrderService;
import com.yonyou.brigade.third_parties.fanscar.service.IFanscarPublicService;
import com.yonyou.brigade.util.operateutil.DateUtils;
import com.yonyou.ybzboot.starter.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: kfw
 * @Date: 2019/2/28 20:00
 * @Description:
 */
@RestController
@RequestMapping("/fanscar")
public class FanscarOrderController {

    @Autowired
    private IFanscarOrderService fanscarOrderService;

    @Autowired
    private IFanscarPublicService fanscarPublicService;

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService channelService;

    @RequestMapping(path = "/getFanscarFlightOrders", method = RequestMethod.POST)
    public String getFanscarFlightOrders(@RequestBody Map<String,String> paramMap) {
        String tenant = paramMap.get("tenant");
        Date begin = DateUtils.strToDateTime(paramMap.get("begin"));
        Date end = DateUtils.strToDateTime(paramMap.get("end"));
        List<BriChannel> briChannels = channelService.getEnableChannelByChannel(ChannelEnum.FANSCAR.getName());

        try {
            for (BriChannel briChannel : briChannels) {
                FanscarConfig fanscarConfig = (FanscarConfig) channelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), briChannel.getOrgpk(), new FanscarConfig());
                fanscarOrderService.getFanscarFlightOrders(fanscarConfig, tenant, begin, end, briChannel.getId());
                return "success";
            }
        } catch (Exception e) {
            throw new CustomException("获取泛嘉机票订单失败");
        }
        return "failure";
    }

    @RequestMapping(path = "/getFanscarHotelOrders", method = RequestMethod.POST)
    public String getFanscarHotelOrders(@RequestBody Map<String,String> paramMap) {
        String tenant = paramMap.get("tenant");
        Date begin = DateUtils.strToDateTime(paramMap.get("begin"));
        Date end = DateUtils.strToDateTime(paramMap.get("end"));
        List<BriChannel> briChannels = channelService.getEnableChannelByChannel(ChannelEnum.FANSCAR.getName());

        try {
            for (BriChannel briChannel : briChannels) {
                FanscarConfig fanscarConfig = (FanscarConfig) channelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), briChannel.getOrgpk(), new FanscarConfig());
                fanscarOrderService.getFanscarHotelOrders(fanscarConfig, tenant, begin, end, briChannel.getId());
                return "success";
            }
        } catch (Exception e) {
            throw new CustomException("获取泛嘉酒店订单失败");
        }
        return "failure";
    }

    @RequestMapping(path = "/getFanscarTrainOrders", method = RequestMethod.POST)
    public String getFanscarTrainOrders(@RequestBody Map<String,String> paramMap) {
        String tenant = paramMap.get("tenant");
        Date begin = DateUtils.strToDateTime(paramMap.get("begin"));
        Date end = DateUtils.strToDateTime(paramMap.get("end"));
        List<BriChannel> briChannels = channelService.getEnableChannelByChannel(ChannelEnum.FANSCAR.getName());

        try {
            for (BriChannel briChannel : briChannels) {
                FanscarConfig fanscarConfig = (FanscarConfig) channelService.getChannelConfig(tenant, ChannelEnum.FANSCAR.getName(), briChannel.getOrgpk(), new FanscarConfig());
                fanscarOrderService.getFanscarTrainOrders(fanscarConfig, tenant, begin, end, briChannel.getId());
                return "success";
            }
        } catch (Exception e) {
            throw new CustomException("获取泛嘉火车票订单失败");
        }
        return "failure";
    }

    @RequestMapping(path = "/pushApply", method = RequestMethod.POST)
    public JSONObject pushApply(@RequestBody JSONObject param) {
        String tenantId = param.getString("tenantId");
        String orgPk = param.getString("orgPk");
        BriChannel channel = channelService.getEnableInfoByTenantOrg(tenantId, orgPk, ChannelEnum.FANSCAR.getName());

        return fanscarPublicService.pushApplayOrder(channel, tenantId, orgPk, param.getJSONObject("data"));
    }

    @RequestMapping(path = "/closeApply", method = RequestMethod.POST)
    public List closeApply(@RequestBody JSONObject param) {
        String tenantId = param.getString("tenantId");
        String orgPk = param.getString("orgPk");
        String applicationNumber = param.getString("applicationNumber");

        BriChannel fanscarChannel = channelService.getEnableChannelByPersonTenantOrgAndChannel(tenantId, orgPk, ChannelEnum.FANSCAR.getName());

        return fanscarPublicService.closeApplyOrder(applicationNumber, fanscarChannel);
    }

}
